package com.yusong.test.listener;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.context.event.ContextStoppedEvent;
import org.springframework.stereotype.Service;

/**
 * Created by yusong on 2017/3/7.
 */
@Service
public class ContextStartedEventListener implements ApplicationListener {


    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if(event instanceof ContextStartedEvent){
            System.out.println("ContextStartedEventListener|ContextStartedEvent");
        }else if (event instanceof ContextStoppedEvent){
            System.out.println("ContextStartedEventListener|ContextStoppedEvent");
        }else if (event instanceof ContextRefreshedEvent){
            //只有这个有效
            System.out.println("ContextStartedEventListener|ContextRefreshedEvent");
        }else if (event instanceof ContextClosedEvent){
            //如web容器关闭时自动会触发spring容器的关闭，如果是普通java应用，需要调用ctx.registerShutdownHook();注册虚拟机关闭时的钩子才行
            System.out.println("ContextStartedEventListener|ContextClosedEvent");
        }
    }
}
